package com.xca.is.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.xca.is.R;
import com.xca.is.databinding.TrainNumberItemBinding;
import com.xca.is.entity.TrainEntity;

import java.util.ArrayList;
import java.util.List;

public class TrainAdapter extends RecyclerView.Adapter<TrainAdapter.ViewHolder>{
    private List<TrainEntity.DataDTO> list ;
    private TrainSeatAdapter adapter ;
    private Context context ;
    private boolean isShow = false ;
    private List<TrainEntity.DataDTO.SeatListDTO> seatList ;

    public TrainAdapter(List<TrainEntity.DataDTO> list, Context context) {
        this.list = list;
        this.context = context;
    }
    public void updateList(List<TrainEntity.DataDTO> list){
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context,R.layout.train_number_item , null) ;
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TrainEntity.DataDTO data = list.get(position) ;
        holder.recyclerView.setVisibility(View.GONE);
        seatList = new ArrayList<>();
        seatList = data.getSeatList() ;
        holder.recyclerView.setLayoutManager(new GridLayoutManager(context,1));
        ViewGroup.LayoutParams layoutParams = holder.recyclerView.getLayoutParams() ;
        layoutParams.height = 180 * seatList.size() ;
        holder.recyclerView.setLayoutParams(layoutParams);
        adapter = new TrainSeatAdapter(context,seatList) ;
        holder.recyclerView.setAdapter(adapter);
        holder.start.setText(data.getStart());
        holder.depart.setText(data.getDepartTime());
        holder.end.setText(data.getEnd());
        holder.arrive.setText(data.getArriveTime());
        holder.number.setText(data.getTrainNumber());
        holder.time.setText(data.getTimeDifference());

        holder.look.setSelected(false);

        holder.look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("seatList数量", String.valueOf(seatList.size()));
                holder.look.setSelected(!isShow);
                if(isShow){
                    holder.recyclerView.setVisibility(View.GONE);
                }else{
                    holder.recyclerView.setVisibility(View.VISIBLE);
                }
                isShow = !isShow ;
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.d("获取数据--train", String.valueOf(list.size()));
        return list.isEmpty() ?0:list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView start , depart , number , time , end ,arrive ;
        private RecyclerView recyclerView ;
        private ImageButton look ;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            start = itemView.findViewById(R.id.train_item_start) ;
            depart = itemView.findViewById(R.id.train_item_depart) ;
            number = itemView.findViewById(R.id.train_item_number) ;
            time = itemView.findViewById(R.id.train_item_time) ;
            end = itemView.findViewById(R.id.train_item_end) ;
            arrive = itemView.findViewById(R.id.train_item_arrive) ;
            recyclerView = itemView.findViewById(R.id.train_item_seats);
            look = itemView.findViewById(R.id.train_item_look);
        }
    }




    public static class TrainSeatAdapter extends RecyclerView.Adapter<TrainSeatAdapter.ViewHolder>{
        private List<TrainEntity.DataDTO.SeatListDTO> list ;
        private Context context ;

        public TrainSeatAdapter(Context context, List<TrainEntity.DataDTO.SeatListDTO> list) {
            this.context = context;
            this.list = list;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(context,R.layout.train_seat_item , null) ;
            return new ViewHolder(view);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            TrainEntity.DataDTO.SeatListDTO data = list.get(position) ;
            holder.name.setText(data.getSeatName());
            holder.price.setText("￥" +String.valueOf(data.getSeatPrice()));
            holder.num.setText(String.valueOf(data.getSeatresidue()) + "张");
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        static class ViewHolder extends RecyclerView.ViewHolder{
            TextView name , price , num ;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.train_seat_name);
                price = itemView.findViewById(R.id.train_seat_price);
                num = itemView.findViewById(R.id.train_seat_num);
            }
        }
    }
}
